//
//  ReadFontView.swift
//  WalkNovel
//
//  Created by Kevin on 2023/7/14.
//

import UIKit

class ReadFontView: UIView {
    
    lazy var sizeView: UIView = {
        let view = UIView()
        view.wn_cornerWithBorder(20)
        return view
    }()
    
    lazy var sizeLineView: UIView = {
        return UIView()
    }()
        
    lazy var spaceView: UIView = {
        let view = UIView()
        view.wn_cornerWithBorder(20)
        return view
    }()
    
    lazy var spaceLineView: UIView = {
        return UIView()
    }()
    
    lazy var sizeSubBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("Aa-", for: .normal)
        btn.titleLabel?.font = .wn_font(name: .bold, size: 16)
        btn.titleLabel?.textAlignment = .center
        btn.rx.tap.bind { [weak self] in
            self?.changeSize(false)
        }.disposed(by: rx.disposeBag)
        return btn
    }()
    
    lazy var sizeAddBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("Aa+", for: .normal)
        btn.titleLabel?.font = .wn_font(name: .bold, size: 16)
        btn.titleLabel?.textAlignment = .center
        btn.rx.tap.bind { [weak self] in
            self?.changeSize(true)
        }.disposed(by: rx.disposeBag)
        return btn
    }()
    
    lazy var spaceSubBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.imageEdgeInsets = .init(top: 13, left: 21.5, bottom: 13, right: 21.5)
        btn.rx.tap.bind { [weak self] in
            self?.changeSpace(false)
        }.disposed(by: rx.disposeBag)
        return btn
    }()
    
    lazy var spaceAddBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.imageEdgeInsets = .init(top: 13, left: 21.5, bottom: 13, right: 21.5)
        btn.rx.tap.bind { [weak self] in
            self?.changeSpace(true)
        }.disposed(by: rx.disposeBag)
        return btn
    }()
    
    lazy var bgWhiteBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("", for: .normal)
        btn.setTitleColor(.black, for: .normal)
        btn.titleLabel?.textAlignment = .center
        btn.titleLabel?.font = .wn_font(name: .bold, size: 16)
        btn.rx.tap.bind { [weak self] in
            self?.changeBgColor("FFFFFF")
        }.disposed(by: rx.disposeBag)
        btn.wn_cornerWithBorder(14)
        return btn
    }()
    
    lazy var whiteImage: UIImageView = {
        return UIImageView(image: UIImage(named: "read_setting_bottom_sel"))
    }()
    
    lazy var bgYellowBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("", for: .normal)
        btn.setTitleColor(.black, for: .normal)
        btn.titleLabel?.textAlignment = .center
        btn.titleLabel?.font = .wn_font(name: .bold, size: 16)
        btn.rx.tap.bind { [weak self] in
            self?.changeBgColor("FFECCE")
        }.disposed(by: rx.disposeBag)
        btn.wn_cornerWithBorder(14)
        return btn
    }()
    
    lazy var yellowImage: UIImageView = {
        return UIImageView(image: UIImage(named: "read_setting_bottom_sel"))
    }()
    
    lazy var bgGreenBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("", for: .normal)
        btn.setTitleColor(.black, for: .normal)
        btn.titleLabel?.textAlignment = .center
        btn.titleLabel?.font = .wn_font(name: .bold, size: 16)
        btn.rx.tap.bind { [weak self] in
            self?.changeBgColor("DDE4D6")
        }.disposed(by: rx.disposeBag)
        btn.wn_cornerWithBorder(14)
        return btn
    }()
    
    lazy var greenImage: UIImageView = {
        return UIImageView(image: UIImage(named: "read_setting_bottom_sel"))
    }()
    
    lazy var defFontBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("預設", for: .normal)
        btn.titleLabel?.textAlignment = .center
        btn.titleLabel?.font = .wn_font(name: .regular, size: 16)
        btn.setImage(UIImage(named: "read_setting_bottom_sel"), for: .normal)
        btn.rx.tap.bind { [weak self] in
            self?.changeFont("regular")
        }.disposed(by: rx.disposeBag)
        return btn
    }()
    
    lazy var floFontBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("海報", for: .normal)
        btn.titleLabel?.textAlignment = .center
        btn.titleLabel?.font = .wn_font(name: .floane, size: 16)
        btn.setImage(UIImage(named: "read_setting_bottom_sel"), for: .normal)
        btn.rx.tap.bind { [weak self] in
            self?.changeFont("floane")
        }.disposed(by: rx.disposeBag)
        return btn
    }()
    
    lazy var natFontBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("自然", for: .normal)
        btn.titleLabel?.textAlignment = .center
        btn.titleLabel?.font = .wn_font(name: .national, size: 16)
        btn.setImage(UIImage(named: "read_setting_bottom_sel"), for: .normal)
        btn.rx.tap.bind { [weak self] in
            self?.changeFont("national")
        }.disposed(by: rx.disposeBag)
        return btn
    }()
    
    lazy var medFontBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("中黑", for: .normal)
        btn.titleLabel?.textAlignment = .center
        btn.titleLabel?.font = .wn_font(name: .medium, size: 16)
        btn.setImage(UIImage(named: "read_setting_bottom_sel"), for: .normal)
        btn.rx.tap.bind { [weak self] in
            self?.changeFont("medium")
        }.disposed(by: rx.disposeBag)
        return btn
    }()

    var currentSize: CGFloat = 16 {
        didSet {
            sizeAddBtn.isEnabled = currentSize < maxSize
            sizeSubBtn.isEnabled = currentSize > minSize
        }
    }
    
    var minSize: CGFloat = 12

    var maxSize: CGFloat = 26
        
    var currentSpace: CGFloat = 5.0 {
        didSet {
            spaceAddBtn.isEnabled = currentSpace < maxSpace
            spaceSubBtn.isEnabled = currentSpace > minSpace
        }
    }
    
    var minSpace: CGFloat = 1.0

    var maxSpace: CGFloat = 17.0
        
    var bgColorItem = "FFFFFF"

    var fontItem = "regular"
        
    var changeFontClosure: BoolClosure?

    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(sizeView)
        sizeView.addSubview(sizeSubBtn)
        sizeView.addSubview(sizeAddBtn)
        sizeView.addSubview(sizeLineView)
        addSubview(spaceView)
        spaceView.addSubview(spaceAddBtn)
        spaceView.addSubview(spaceSubBtn)
        spaceView.addSubview(spaceLineView)
        addSubview(bgYellowBtn)
        addSubview(yellowImage)
        addSubview(bgWhiteBtn)
        addSubview(whiteImage)
        addSubview(bgGreenBtn)
        addSubview(greenImage)
        addSubview(floFontBtn)
        addSubview(defFontBtn)
        addSubview(natFontBtn)
        addSubview(medFontBtn)
        sizeView.snp.makeConstraints { make in
            make.top.equalTo(16)
            make.left.equalTo(20)
            make.size.equalTo(CGSize.init(width: 143, height: 40))
        }
        sizeSubBtn.snp.makeConstraints { make in
            make.top.left.bottom.equalTo(0)
            make.width.equalTo(71)
        }
        sizeAddBtn.snp.makeConstraints { make in
            make.top.bottom.right.equalTo(0)
            make.width.equalTo(71)
        }
        sizeLineView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.equalTo(1)
            make.height.equalTo(20)
        }
        spaceView.snp.makeConstraints { make in
            make.top.equalTo(16)
            make.right.equalTo(-20)
            make.size.equalTo(CGSize.init(width: 143, height: 40))
        }
        spaceAddBtn.snp.makeConstraints { make in
            make.top.bottom.right.equalTo(0)
            make.width.equalTo(71)
        }
        spaceSubBtn.snp.makeConstraints { make in
            make.top.left.bottom.equalTo(0)
            make.width.equalTo(71)
        }
        spaceLineView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.equalTo(1)
            make.height.equalTo(20)
        }
        bgYellowBtn.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.size.equalTo(CGSize.init(width: 90, height: 28))
        }
        yellowImage.snp.makeConstraints { make in
            make.centerX.equalTo(bgYellowBtn.snp.centerX)
            make.top.equalTo(bgYellowBtn.snp.bottom).offset(3)
            make.size.equalTo(CGSize.init(width: 6, height: 4))
        }
        bgWhiteBtn.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(20)
            make.size.equalTo(bgYellowBtn.snp.size)
        }
        whiteImage.snp.makeConstraints { make in
            make.centerX.equalTo(bgWhiteBtn.snp.centerX)
            make.top.equalTo(bgWhiteBtn.snp.bottom).offset(3)
            make.size.equalTo(CGSize.init(width: 6, height: 4))
        }
        bgGreenBtn.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(-20)
            make.size.equalTo(bgYellowBtn.snp.size)
        }
        greenImage.snp.makeConstraints { make in
            make.centerX.equalTo(bgGreenBtn.snp.centerX)
            make.top.equalTo(bgGreenBtn.snp.bottom).offset(3)
            make.size.equalTo(CGSize.init(width: 6, height: 4))
        }
        floFontBtn.snp.makeConstraints { make in
            make.centerX.equalToSuperview().offset(-42)
            make.bottom.equalTo(-24)
        }
        defFontBtn.snp.makeConstraints { make in
            make.centerY.equalTo(floFontBtn.snp.centerY)
            make.left.equalTo(24)
        }
        natFontBtn.snp.makeConstraints { make in
            make.centerY.equalTo(floFontBtn.snp.centerY)
            make.right.equalTo(-24)
        }
        medFontBtn.snp.makeConstraints { make in
            make.centerX.equalToSuperview().offset(42)
            make.bottom.equalTo(-24)
        }
        floFontBtn.layoutButton(style: .bottom, imageTitleSpace: 4)
        defFontBtn.layoutButton(style: .bottom, imageTitleSpace: 4)
        natFontBtn.layoutButton(style: .bottom, imageTitleSpace: 4)
        medFontBtn.layoutButton(style: .bottom, imageTitleSpace: 4)
        wn_cornerWithBorder(20)
    }
    
    func darkModeConfig() {
        backgroundColor = .init(named: isDark() ? "black_1B1B1B" : "white_F9F8F8")
        currentSize = getReadConfigItem("size") as? CGFloat ?? 16.0
        currentSpace = getReadConfigItem("space") as? CGFloat ?? 5.0
        bgColorItem = getReadConfigItem("bgColor") as? String ?? "FFFFFF"
        fontItem = getReadConfigItem("font") as? String ?? "regular"

        sizeView.backgroundColor = isDark() ? .init(named:  "black_212121") : .white
        sizeLineView.backgroundColor = .init(named: isDark() ? "black_1B1B1B" : "white_F3F3F3")
        spaceView.backgroundColor = isDark() ? .init(named:  "black_212121") : .white
        spaceLineView.backgroundColor = .init(named: isDark() ? "black_1B1B1B" : "white_F3F3F3")
        sizeSubBtn.setTitleColor(.init(named: sizeSubBtn.isEnabled ? ((isDark() ? "gray_999999" : "black_333333")) : ((isDark() ? "gray_666666" : "gray_999999"))), for: .normal)
        sizeAddBtn.setTitleColor(.init(named: sizeAddBtn.isEnabled ? ((isDark() ? "gray_999999" : "black_333333")) : ((isDark() ? "gray_666666" : "gray_999999"))), for: .normal)
        
        spaceSubBtn.setImage(spaceSubBtn.isEnabled ? UIImage(named: isDark() ? "read_setting_lineSpace_reduce_night" : "read_setting_lineSpace_reduce")?.withTintColor(.init(named: (isDark() ? "gray_999999" : "black_333333"))!, renderingMode: .alwaysOriginal) : UIImage(named: isDark() ? "read_setting_lineSpace_reduce_night" : "read_setting_lineSpace_reduce")?.withTintColor(.init(named: (isDark() ? "gray_666666" : "gray_999999"))!, renderingMode: .alwaysOriginal), for: .normal)
        spaceAddBtn.setImage(spaceAddBtn.isEnabled ? UIImage(named: isDark() ? "read_setting_lineSpace_add_night" : "read_setting_lineSpace_add")?.withTintColor(.init(named: (isDark() ? "gray_999999" : "black_333333"))!, renderingMode: .alwaysOriginal) : UIImage(named: isDark() ? "read_setting_lineSpace_add_night" : "read_setting_lineSpace_add")?.withTintColor(.init(named: (isDark() ? "gray_666666" : "gray_999999"))!, renderingMode: .alwaysOriginal), for: .normal)
        
        bgWhiteBtn.backgroundColor = .init(white: 1, alpha: isDark() ? 0.6 : 1)
        whiteImage.isHidden = bgColorItem != "FFFFFF"
        bgYellowBtn.backgroundColor = .wn_colorWithHexString("FFECCE", isDark() ? 0.6 : 1)
        yellowImage.isHidden = bgColorItem != "FFECCE"
        bgGreenBtn.backgroundColor = .wn_colorWithHexString("DDE4D6", isDark() ? 0.6 : 1)
        greenImage.isHidden = bgColorItem != "DDE4D6"
        
        defFontBtn.setTitleColor(showColor("regular"), for: .normal)
        floFontBtn.setTitleColor(showColor("floane"), for: .normal)
        natFontBtn.setTitleColor(showColor("national"), for: .normal)
        medFontBtn.setTitleColor(showColor("medium"), for: .normal)
        defFontBtn.imageView?.isHidden = fontItem != "regular"
        floFontBtn.imageView?.isHidden = fontItem != "floane"
        natFontBtn.imageView?.isHidden = fontItem != "national"
        medFontBtn.imageView?.isHidden = fontItem != "medium"
    }
    
    func changeSize(_ add: Bool) {
        if add {
            currentSize += 2
        }else {
            currentSize -= 2
        }
        saveReadConfigItem("size", currentSize)
        darkModeConfig()
        changeFontClosure?(true)
    }
    
    func changeSpace(_ add: Bool) {
        if add {
            currentSpace += 2.0
        }else {
            currentSpace -= 2.0
        }
        saveReadConfigItem("space", currentSpace)
        darkModeConfig()
        changeFontClosure?(true)
    }
    
    func changeBgColor(_ item: String) {
        bgColorItem = item
        saveReadConfigItem("bgColor", bgColorItem)
        darkModeConfig()
        changeFontClosure?(false)
    }
    
    func changeFont(_ item: String) {
        fontItem = item
        saveReadConfigItem("font", fontItem)
        darkModeConfig()
        changeFontClosure?(true)
    }
    
    func showColor(_ item: String) -> UIColor {
        return UIColor.init(named: fontItem == item ? "red_FE6390" : (isDark() ? "gray_666666" : "black_333333")) ?? .black
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
